package game.game.network.server;

import game.engine.network.cmd.Command;
import game.engine.network.cmd.EnumCommand;
import game.engine.network.server.IControllerServer;
import game.game.network.cmd.CmdCGetCacheMap;
import game.game.network.cmd.CmdCGetObject;
import game.game.network.cmd.CmdCUpdatePosition;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.List;

import json.JSONException;
import json.JSONObject;

public class ServerThread extends Thread
{

	private Socket _socket;

	private PrintWriter _streamOutput;

	private IControllerServer _controller;

	protected GameServer _gameServer;

	protected Integer _id;

	public ServerThread(Integer pId, GameServer pGameServer, Socket pSocket, IControllerServer pController)
	{
		this._socket = pSocket;
		this._id = pId;
		this._gameServer = pGameServer;
		try
		{
			_streamOutput = new PrintWriter( _socket.getOutputStream() );
		}
		catch ( IOException e )
		{
			e.printStackTrace();
		}
		//
		_controller = pController;
	}

	public void run()
	{
		try
		{
			// comande init
			BufferedReader ois = new BufferedReader( new InputStreamReader( _socket.getInputStream() ) );
			String cmd = null;
			while ( (cmd = ois.readLine()) != null )
			{
				System.out.println( "receive string" + cmd );
				try
				{
					JSONObject jSONObject = new JSONObject( cmd );
					int objId = jSONObject.getInt( Command.KEY_IDCMD );
					if ( objId == EnumCommand.CMD_CLIENT_GET_CACHE_MAP.getValue() )
					{
						new CmdCGetCacheMap( jSONObject ).execute( _controller );
					}
					if ( objId == EnumCommand.CMD_CLIENT_GET_OBJECT.getValue() )
					{
						new CmdCGetObject( jSONObject ).execute( _controller );
					}
					if ( objId == EnumCommand.CMD_CLIENT_UPDATE_POSITION.getValue() )
					{
						new CmdCUpdatePosition( jSONObject ).execute( _controller );
					}
				}
				catch ( JSONException e )
				{
					System.err.println( e.getMessage() );
					e.printStackTrace();
					System.err.println( cmd );
				}
			}
			ois.close();
		}

		catch ( Exception e )
		{
			System.err.println( "deconnection et pb de socket" );

		}
		close();
	}

	private void close()
	{
		try
		{
			_streamOutput.close();
		}
		catch ( Exception e1 )
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if ( _socket != null )
		{
			try
			{
				_socket.close();
			}
			catch ( IOException e )
			{
				e.printStackTrace();
			}
		}
		_gameServer.deconnectThread( _id );
	}

	public void sendCommand( Command pCmd )
	{
		try
		{
			_streamOutput.println( pCmd.getCmdString() );
			_streamOutput.flush();
		}
		catch ( Exception e )
		{
			// TODO Auto-generated catch block
			// e.printStackTrace();
			System.err.println( "deconnection et pb de socket" );
		}

	}

	public void sendCommands( List<Command> pListCmd )
	{
		for ( Command cmd : pListCmd )
		{
			sendCommand( cmd );
		}
	}

}
